'use strict';
const db = uniCloud.database()

exports.main = async (event, context) => {
  try {
    console.log('开始初始化物种数据...');
    
    // 获取数据库实例
    const collection = db.collection('species')
    console.log('获取到数据库集合');
    
    // 测试数据
    const testData = [
      {
        _id: '67c9145999c62422a8e87525',
        name: '豹纹守宫',
        scientificName: 'Eublepharis macularius',
        category: '爬行类',
        threatLevel: '无危',
        origin: '巴基斯坦、阿富汗、伊朗',
        images: [
          'https://example.com/leopard-gecko-1.jpg',
          'https://example.com/leopard-gecko-2.jpg'
        ],
        features: {
          size: '20-25厘米',
          lifespan: '10-20年',
          description: '豹纹守宫是一种小型蜥蜴，以其温顺的性格和独特的斑点花纹而闻名。',
          habitat: '沙漠和半沙漠地区',
          diet: '昆虫、小型无脊椎动物'
        },
        careGuide: {
          temperature: '28-32℃',
          humidity: '30-40%',
          lighting: '需要UVB照明',
          substrate: '沙子或爬虫垫',
          feeding: '每周2-3次'
        },
        legalNotice: {
          status: '可合法饲养',
          requirements: '需要饲养许可证',
          restrictions: '禁止野外捕捉'
        },
        createTime: new Date(),
        updateTime: new Date()
      },
      {
        _id: '67c9145999c62422a8e87526',
        name: '非洲迷你刺猬',
        scientificName: 'Atelerix albiventris',
        category: '小型哺乳动物类',
        threatLevel: '无危',
        origin: '非洲中部和东部',
        images: [
          'https://example.com/hedgehog-1.jpg',
          'https://example.com/hedgehog-2.jpg'
        ],
        features: {
          size: '15-20厘米',
          lifespan: '3-6年',
          description: '非洲迷你刺猬是一种小型哺乳动物，以其可爱的外表和温顺的性格而受欢迎。',
          habitat: '草原和灌木丛',
          diet: '昆虫、水果、蔬菜'
        },
        careGuide: {
          temperature: '24-27℃',
          humidity: '40-60%',
          lighting: '自然光即可',
          substrate: '木屑或纸屑',
          feeding: '每天一次'
        },
        legalNotice: {
          status: '可合法饲养',
          requirements: '需要饲养许可证',
          restrictions: '禁止野外捕捉'
        },
        createTime: new Date(),
        updateTime: new Date()
      },
      {
        _id: '67c9145999c62422a8e87527',
        name: '红眼树蛙',
        scientificName: 'Agalychnis callidryas',
        category: '两栖类',
        threatLevel: '近危',
        origin: '中美洲',
        images: [
          'https://example.com/red-eye-frog-1.jpg',
          'https://example.com/red-eye-frog-2.jpg'
        ],
        features: {
          size: '5-7厘米',
          lifespan: '5-10年',
          description: '红眼树蛙以其鲜艳的红色眼睛和绿色身体而闻名，是一种受欢迎的观赏蛙类。',
          habitat: '热带雨林',
          diet: '昆虫、小型无脊椎动物'
        },
        careGuide: {
          temperature: '24-28℃',
          humidity: '70-80%',
          lighting: '需要UVB照明',
          substrate: '椰土或苔藓',
          feeding: '每周3-4次'
        },
        legalNotice: {
          status: '需要特殊许可',
          requirements: '需要饲养许可证和物种证明',
          restrictions: '禁止野外捕捉和贸易'
        },
        createTime: new Date(),
        updateTime: new Date()
      }
    ]

    console.log('准备清空现有数据...');
    // 清空现有数据
    const removeResult = await collection.remove()
    console.log('清空数据结果：', removeResult);
    
    console.log('准备插入测试数据...');
    // 插入测试数据
    const result = await collection.add(testData)
    console.log('插入数据结果：', result);
    
    return {
      code: 200,
      message: '物种数据初始化成功',
      data: result
    }
  } catch (error) {
    console.error('初始化失败，错误信息：', error);
    return {
      code: 500,
      message: '初始化失败：' + error.message,
      error: error
    }
  }
} 